package com.ss.android.video;

import static com.ss.android.video.SSMediaPlayerWrapper.CALLBACK_ON_BUFFERING_UPDATE;
import static com.ss.android.video.SSMediaPlayerWrapper.CALLBACK_ON_COMPLETE;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.article.browser.R;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.newmedia.activity.BaseActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public abstract class bj extends BaseActivity
        implements MediaPlayer.OnCompletionListener,
        MediaPlayer.OnErrorListener,
        MediaPlayer.OnPreparedListener,
        MediaPlayer.OnVideoSizeChangedListener,
        Callback {
    protected MediaPlayer e;
    protected WeakHandler f;
    protected boolean g;
    protected List h;
    protected long i;
    protected String j;
    private SurfaceView a;
    private SurfaceHolder b;
    private boolean c;
    private boolean d;
    private int k;
    private int l;
    private int m;
    private int n;
    private int o;
    private int p;
    private int q;
    private SurfaceHolder.Callback r;

    public bj() {
        this.f = new WeakHandler(((Callback) this));
        this.g = false;
        this.c = false;
        this.d = true;
        this.h = new ArrayList();
        this.q = 0;
        this.i = -1;
        this.r = new SurfaceHolder.Callback() {

            public final void surfaceChanged(SurfaceHolder surfaceHolder, int arg3, int arg4, int arg5) {
                surfaceHolder.setType(3);
                try {
                    bj.this.e.setDisplay(surfaceHolder);
                } catch (Throwable v0) {
                }
            }

            public final void surfaceCreated(SurfaceHolder arg1) {
            }

            public final void surfaceDestroyed(SurfaceHolder arg1) {
            }
        };
    }

    protected static String a(String arg13, HashMap arg14) {
        URLConnection v3 = null;
        Object v2_1 = null;
        Object v1_1;
        int v9 = 10;
        String v6 = null;
        String v1 = "";
        String v2 = "";
        HashMap v10 = new HashMap();
        if (arg14 != null) {
            v1_1 = arg14.get("Referer");
            v2_1 = arg14.get("User-Agent");
            ((Map) v10).putAll(((Map) arg14));
            if (!android.support.design.a.isTextEmpty(((String) v1_1))) {
                ((Map) v10).remove("Referer");
            }

            if (android.support.design.a.isTextEmpty(((String) v2_1))) {
            } else {
                ((Map) v10).remove("User-Agent");
            }
        }

        String v4 = ((String) v2_1);
        String v5 = v1;
        int v8 = v9;
        String v7 = arg13;
        while (v8 > 0) {
            try {
                v3 = new URL(v7).openConnection();
                if (v3 != null) {
                    try {
                        label_36:
                        ((HttpURLConnection) v3).addRequestProperty("Cache-Control", "no-cache");
                        if (!android.support.design.a.isTextEmpty(v5)) {
                            ((HttpURLConnection) v3).setRequestProperty("Referer", v5);
                        }

                        if (!android.support.design.a.isTextEmpty(v4)) {
                            ((HttpURLConnection) v3).setRequestProperty("User-Agent", v4);
                        }

                        if (v8 == v9) {
                            Iterator v11 = ((Map) v10).keySet().iterator();
                            while (v11.hasNext()) {
                                v1_1 = v11.next();
                                ((HttpURLConnection) v3).addRequestProperty(((String) v1_1), (String) ((Map) v10).get(v1_1));
                            }
                        }

                        ((HttpURLConnection) v3).setDefaultUseCaches(false);
                        ((HttpURLConnection) v3).setInstanceFollowRedirects(false);
                        ((HttpURLConnection) v3).connect();
                        int v1_4 = ((HttpURLConnection) v3).getResponseCode();
                        if (v1_4 != 200) {
                            label_72:
                            if (v1_4 != CALLBACK_ON_BUFFERING_UPDATE && v1_4 != CALLBACK_ON_COMPLETE && v1_4 != 307) {
                                if (v3 != null) {
                                    ((HttpURLConnection) v3).disconnect();
                                }
                                return v6;
                            } else {
                                v1 = ((HttpURLConnection) v3).getHeaderField("Location");
                                if (!android.support.design.a.isTextEmpty(v1)) {
                                    try {
                                        label_85:
                                        if (!TTUtils.isValidUrl(v1)) {
                                            int v2_2 = v7.lastIndexOf(47);
                                            v7 = v2_2 >= 0 ? v7.substring(0, v2_2 + 1) + v1 : v7 + "/" + v1;
                                        } else {
                                            v7 = v1;
                                        }
                                    } catch (Exception v1_3) {
                                        return v6;
                                    } finally {
                                        if (v3 != null) {
                                            ((HttpURLConnection) v3).disconnect();
                                        }
                                    }
                                    --v8;
                                    continue;
                                } else {
                                    if (v3 != null) {
                                        ((HttpURLConnection) v3).disconnect();
                                    }
                                    return v6;
                                }
                            }
                        }
                    } catch (Exception v1_3) {
                        return v6;
                    } finally {
                        if (v3 != null) {
                            ((HttpURLConnection) v3).disconnect();
                        }
                    }
                    return v7;
                }

                v6 = "";
                if (v3 == null) {
                    return v6;
                }
            } catch (Exception v1_3) {
                return v6;
            } finally {
                if (v3 != null) {
                    ((HttpURLConnection) v3).disconnect();
                }
            }
            return v6;


        }

        return v6;
    }

    protected abstract SurfaceView a();

    protected final void a(int arg11, String arg12) {
        Object v0_1;
        int v9 = R.string.pr;
        int v8 = R.drawable.close_popup_textpage;
        if (!this.isViewValid()) {
            return;
        }

        if (!NetworkUtils.isNetworkAvailable(((Context) this))) {
            UIUtils.showImageToast(((Context) this), v8, R.string.net_weak);
            this.onBackPressed();
            return;
        }

        switch (arg11) {
            case 100: {
                if (this.h.size() > this.q) {
                    v0_1 = this.h.get(this.q);
                    new c(this.f, ((d) v0_1).a, ((d) v0_1).d).start();
                    return;
                }
                new bj$a(this, this.j, "", "", this.i).start();
                UIUtils.showImageToast(((Context) this), v8, v9);
                this.onBackPressed();
                return;
            }
            case 101: {
                new bj$a(this, this.j, "", "", this.i).start();
                UIUtils.showImageToast(((Context) this), v8, v9);
                this.onBackPressed();
                return;
            }
            case 102: {
                this.g = false;
                if (arg12.startsWith("file://")) {
                    arg12 = arg12.substring(7);
                }
                this.e.reset();
                try {
                    this.e.setDataSource(arg12);
                } catch (Exception v0) {
                    return;
                }
                this.e.prepareAsync();
                return;
            }
            case 103: {
                String v3 = this.j;
                String v4 = "";
                String v5 = "";
                if (this.h.size() > this.q) {
                    v0_1 = this.h.get(this.q);
                    v3 = ((d) v0_1).a;
                    v4 = ((d) v0_1).b;
                    v5 = ((d) v0_1).c;
                }
                new bj$a(this, v3, v4, v5, this.i).start();
                ++this.q;
                if (this.h.size() <= this.q) {
                    UIUtils.showImageToast(((Context) this), v8, v9);
                    this.onBackPressed();
                } else {
                    v0_1 = this.h.get(this.q);
                    new c(this.f, ((d) v0_1).a, ((d) v0_1).d).start();
                }
                return;
            }
        }
        return;
    }

    protected final void a(long arg4) {
        if ((this.g) && this.e != null) {
            this.e.seekTo(((int) arg4));
        }
    }

    protected void a(boolean arg3) {
        int v1 = 1024;
        if (arg3) {
            this.o = this.k;
            this.p = this.l;
            this.getWindow().clearFlags(v1);
        } else {
            this.o = this.l;
            this.p = this.k;
            this.getWindow().addFlags(v1);
        }

        this.h();
    }

    protected final void b() {
        this.e = new MediaPlayer();
        this.e.setOnPreparedListener(((MediaPlayer.OnPreparedListener) this));
        this.e.setOnVideoSizeChangedListener(((MediaPlayer.OnVideoSizeChangedListener) this));
        this.e.setOnCompletionListener(((MediaPlayer.OnCompletionListener) this));
        this.e.setOnErrorListener(((MediaPlayer.OnErrorListener) this));
        this.e.setScreenOnWhilePlaying(true);
        this.a = this.a();
        this.b = this.a.getHolder();
        this.b.addCallback(this.r);
        try {
            this.b.setType(3);
            this.e.setDisplay(this.b);
        } catch (Exception v0) {
        }

        Display v0_1 = this.getWindowManager().getDefaultDisplay();
        this.k = v0_1.getWidth();
        this.l = v0_1.getHeight();
        this.o = this.k;
        this.p = this.l;
    }

    protected final void c() {
        if ((this.g) && !this.e.isPlaying()) {
            this.e.start();
        }
    }

    protected final void d() {
        if ((this.g) && (this.e.isPlaying())) {
            this.e.pause();
        }
    }

    protected final long e() {
        long v0 = !this.g || this.e == null ? 0 : ((long) this.e.getCurrentPosition());
        return v0;
    }

    protected final long f() {
        long v0 = !this.g || this.e == null ? 0 : ((long) this.e.getDuration());
        return v0;
    }

    protected final boolean g() {
        boolean v0 = !this.g || this.e == null ? false : this.e.isPlaying();
        return v0;
    }

    private void h() {
        int v0;
        int v1;
        if (this.m != 0 && this.n != 0) {
            if (this.m >= this.o) {
                v1 = this.o;
                v0 = this.o * this.n / this.m;
                if (v0 > this.p) {
                    v0 = this.p;
                    v1 = this.p * this.m / this.n;
                }
            } else {
                v0 = this.o * this.n / this.m;
                if (v0 > this.p) {
                    v0 = this.p;
                    v1 = this.p * this.m / this.n;
                } else {
                    v1 = this.o;
                }
            }

            this.b.setFixedSize(v1, v0);
            ViewGroup.LayoutParams v2 = this.a.getLayoutParams();
            v2.width = v1;
            v2.height = v0;
            this.a.setLayoutParams(v2);
            this.a.invalidate();
        }
    }

    public void handleMsg(Message message) {
        if (this.isViewValid()) {
            if (message.obj != null && ((message.obj instanceof String))) {
                this.a(message.what, (String) message.obj);
                return;
            }

            this.a(message.what, null);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.e != null) {
            this.e.release();
            this.e = null;
        }
    }

    public void onPause() {
        super.onPause();
        if (this.e != null) {
            this.c = this.e.isPlaying();
            this.e.pause();
        }
    }

    public void onPrepared(MediaPlayer arg2) {
        this.g = true;
    }

    public void onResume() {
        super.onResume();
        if (this.e != null) {
            if (this.c) {
                this.e.start();
                this.c = false;
            } else if (!this.d) {
                this.e.start();
                this.a.post(new Runnable() {

                    public final void run() {
                        bj.this.e.pause();
                    }
                });
            }
        }

        this.d = false;
    }

    public void onVideoSizeChanged(MediaPlayer arg1, int arg2, int arg3) {
        this.m = arg2;
        this.n = arg3;
        this.h();
    }

    public static final class b extends Thread {
        private final bj b;
        private String a;

        public b(bj arg1, String arg2) {
            this.b = arg1;
            this.a = arg2;
        }

        public final void run() {
            d v6;
            JSONObject v5;
            int v1_1;
            String v0_2;
            int v11 = 101;
            Message v2 = this.b.f.obtainMessage();
            int v0 = 30720;
            try {
                v0_2 = NetworkUtils.doGet(v0, this.a);
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    v2.what = 101;
                    this.b.f.sendMessage(v2);
                    return;
                }

                JSONObject v1 = new JSONObject(v0_2);
                if (!"success".equals(v1.optString("message"))) {
                    v2.what = 101;
                    this.b.f.sendMessage(v2);
                    return;
                }

                this.b.i = v1.optLong("video_id", -1);
                JSONArray v3 = v1.getJSONArray("data");
                int v4 = v3.length();
                if (v4 <= 0) {
                    v2.what = 101;
                    this.b.f.sendMessage(v2);
                    return;
                }

                v1_1 = 0;
                while (true) {
                    label_42:
                    if (v1_1 >= v4) {
                        v2.what = 100;
                        this.b.f.sendMessage(v2);
                        return;
                    }

                    v5 = v3.getJSONObject(v1_1);
                    v6 = new d();
                    v6.a = v5.optString("url");
                    JSONObject v7 = v5.optJSONObject("headers");
                    if (v7 == null) {

                    } else {
                        try {
                            Iterator v8 = v7.keys();
                            while (v8.hasNext()) {
                                Object v0_4 = v8.next();
                                v6.d.put(v0_4, v7.getString(((String) v0_4)));
                            }
                            continue;
                        } catch (Exception v0_3) {
                        }
                    }

                    v6.c = v5.optString("site", "");
                    v0_2 = v5.optString("format");
                    if (("mp4".equals(v0_2)) || ("3gp".equals(v0_2))) {
                        v6.b = v0_2;
                        this.b.h.add(v6);
                    }

                    ++v1_1;
                    continue;
                }
            } catch (Throwable v0_1) {
                v2.what = v11;
                this.b.f.sendMessage(v2);
            }
        }
    }

    public static final class d {
        public String a;
        public String b;
        public String c;
        HashMap d;

        protected d() {

            this.d = new HashMap();
        }
    }

    public final class bj$a extends AbsApiThread {
        private String a;
        private String b;
        private String c;
        private Context d;
        private long e;

        public bj$a(Context arg2, String arg3, String arg4, String arg5, long arg6) {
            super(true);
            this.d = arg2.getApplicationContext();
            this.a = arg3;
            this.b = arg4;
            this.c = arg5;
            this.e = arg6;
        }

        public final void run() {
            try {
                String v0_1 = NetworkUtils.getNetworkAccessType(this.d);
                ArrayList v1 = new ArrayList();
                ((List) v1).add(new NameValueImpl("access", v0_1));
                ((List) v1).add(new NameValueImpl("article_video_id", String.valueOf(this.e)));
                ((List) v1).add(new NameValueImpl("format", this.b));
                ((List) v1).add(new NameValueImpl("url", this.a));
                ((List) v1).add(new NameValueImpl("site", this.c));
                NetworkUtils.executePost(20480, "http://ib.snssdk.com/feedback/1/article_video/", ((List) v1));
            } catch (Throwable v0) {
            }
        }
    }

    public final class c extends Thread {
        private final Handler c;
        private String a;
        private HashMap b;

        public c(Handler arg1, String arg2, HashMap arg3) {

            this.c = arg1;
            this.a = arg2;
            this.b = arg3;
        }

        public final void run() {
            Logger.d("video_url", "url=" + this.a);
            String v0 = bj.a(this.a, this.b);
            Logger.d("video_url", "new_url=" + v0);
            Message v1 = this.c.obtainMessage();
            if (android.support.design.a.isTextEmpty(v0)) {
                v1.what = 103;
            } else {
                v1.what = 102;
                v1.obj = v0;
            }

            this.c.sendMessage(v1);
        }
    }
}

